#define  _CRT_SECURE_NO_WARNINGS
class Solution {
public:
    int totalFruit(vector<int>& fruits) {
        int hash[100001]{};
        int l = 0, r = 0;
        int n = fruits.size();
        int ret = 0;
        int cnt = 0;
        while (r < n)
        {
            if (hash[fruits[r]] == 0)
                cnt++;
            hash[fruits[r]]++;

            while (cnt > 2)
            {
                hash[fruits[l]]--;
                if (hash[fruits[l]] == 0)
                    cnt--;
                l++;
            }

            r++;
            ret = max(ret, r - l);
        }
        return ret;
    }
};